package com.vf.admin.miaodong.controller;


import cn.hutool.core.util.ObjectUtil;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.controller.pay.nopay.entity.AppNoPay;
import com.vf.admin.client.controller.pay.nopay.service.AppNoPayService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.*;
import com.vf.admin.miaodong.service.*;
import com.vf.admin.platform.entity.User;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.vf.admin.common.Constants.*;
import static com.vf.admin.utils.RandomUtil.getNewFileNameuuid;
import static com.vf.admin.utils.StringEx.isNull;

/**
 * 用户管理
 */
@Controller
@RequestMapping("/after/user")
public class AfterUserController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AfterUserService afterUserService;
    @Autowired
    AfterGiftRewardService afterGiftRewardService;
    @Autowired
    AfterMingXiShouLiService afterMingXiShouLiService;
    @Autowired
    AfterUserDressupService afterUserDressupService;
    @Autowired
    AfterInvitationRecordService afterInvitationRecordService;
    @Autowired
    AfterGameGiftRecordService afterGameGiftRecordService;
    @Autowired
    AfterMingxiDuihuanService afterMingxiDuihuanService;
    @Autowired
    AfterUserService appUserService;
    @Autowired
    AppUserService userService;
    @Autowired
    AfterDynamicService afterDynamicService;
    @Autowired
    AfterMingxiJinbiService afterMingxiJinbiService;
    @Autowired
    AfterMingXiZuanShiService afterMingXiZuanShiService;
    @Autowired
    AfterFollowService appFollowService;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    AppNoPayService appNoPayService;

    /**
     * 获取所有用户
     */
    @RequestMapping("/orgUserList")
    public String orgUserList(SplitPageBean splitPage, String loginname, String startDate,
                              String endDate, Integer states, String nick,Integer sex,
                              Integer onlineStatus,Integer windowTicket,Integer vipStatus) {

        User users = (User) request.getSession(false).getAttribute(Constants.access_token);
        String code1 = users.getCode();

        try {
            Map<String, Object> map = new HashMap<>();

            if (ObjectUtil.isNotEmpty(code1)) {
                map.put("invitationcode", code1);
            } else {
                map.put("invitationcode", "abcxdadasd");
            }
            map.put("loginname", loginname);
            map.put("startDate", startDate);
            map.put("endDate", endDate);
            map.put("states", states);
            map.put("nick", nick);
            map.put("onlineStatus", onlineStatus);
            map.put("windowTicket", windowTicket);
            map.put("vipStatus", vipStatus);
            map.put("sex", sex); //性别查询

            List<AppUser> maps = afterUserService.findList(map, splitPage);
            for (AppUser user : maps) {
                user.setGuizuname(isNull(user.getNobleid()) || user.getNobleid() > 0 ? guizuMingMap.get(user.getNobleid()) : "");

            }
            //
            Map<Integer, String> sexMap = Constants.getSexMap();
            //
            Map<Integer, String> statesMap = Constants.getStatesMap();
            //在线状态
            Map<Integer, String> onlineStatusMap = Constants.getOnlineStatusMap();
            //门票
            Map<Integer, String> windowTicketMap = Constants.getWindowsTicketMap();
            //vip
            Map<Integer, String> vipStatusMap = Constants.getVipStatusMap();

            request.setAttribute("loginname", loginname);
            request.setAttribute("startDate", startDate);
            request.setAttribute("endDate", endDate);
            request.setAttribute("list", maps);
            request.setAttribute("sexMap", sexMap);
            request.setAttribute("sex", sex);
            request.setAttribute("splitPage", splitPage);
            request.setAttribute("statesMap", statesMap);
            request.setAttribute("onlineStatusMap", onlineStatusMap);
            request.setAttribute("onlineStatus", onlineStatus);
            request.setAttribute("states", states);
            request.setAttribute("nick", nick);
            request.setAttribute("windowTicketMap", windowTicketMap);
            request.setAttribute("windowTicket", windowTicket);
            request.setAttribute("vipStatusMap", vipStatusMap);
            request.setAttribute("vipStatus", vipStatus);
            return this.sucess("/web/after/user/orgUserlist");
        } catch (Exception e) {
            logger.error("查看用户列表异常", e);
            return this.error("系统繁忙");
        }
    }


    /**
     * 获取所有用户
     */
    @RequestMapping("/userList")
    public String getAllUser(SplitPageBean splitPage, String loginname, String startDate,
                             String endDate, Integer status, Integer states, String nick,Integer sex,
                             Integer isReal, Integer onlineStatus,Integer windowTicket,Integer vipStatus) {
                                                                           //门票：1买了 2没买 默认没买
        try {
            User users = (User) request.getSession(false).getAttribute(Constants.access_token);

            Map<String, Object> map = new HashMap<>();
            map.put("loginname", loginname);//登录名
            map.put("nick", nick);//昵称
            map.put("states", states);//账号状态
            map.put("isReal", isReal);//是否真实用户
            map.put("status", status);//实名状态
            map.put("onlineStatus", onlineStatus); //在线状态
            map.put("windowTicket", windowTicket); //
            map.put("vipStatus", vipStatus); //
            map.put("startDate", startDate);
            map.put("endDate", endDate);
            map.put("sex", sex); //性别查询

            List<AppUser> maps = afterUserService.findList(map, splitPage);
            for (AppUser user : maps) {
//                System.out.println(user.getNobleid());
                user.setGuizuname(isNull(user.getNobleid()) || user.getNobleid() > 0 ? guizuMingMap.get(user.getNobleid()) : "");
            }
            //性别
            Map<Integer, String> sexMap = Constants.getSexMap();
            //账号状态
            Map<Integer, String> statesMap = Constants.getStatesMap();
            //实名状态 1已实名  2未实名 默认2
            Map<Integer, String> userStatusMap = Constants.getUserStatusMap();
            //在线状态
            Map<Integer, String> onlineStatusMap = Constants.getOnlineStatusMap();
            //真假用户
            Map<Integer, String> isRealMap = Constants.getIsReal();
            //
            Map<Integer, String> windowTicketMap = Constants.getWindowsTicketMap();

            Map<Integer, String> vipStatusMap = Constants.getVipStatusMap();

            String k = jsonArray.toJSONString(maps);

            request.setAttribute("loginname", loginname);
            request.setAttribute("startDate", startDate);
            request.setAttribute("endDate", endDate);
            request.setAttribute("list", maps);
            request.setAttribute("sexMap", sexMap);
            request.setAttribute("sex", sex);
            request.setAttribute("splitPage", splitPage);
            request.setAttribute("mapsJsonStr", k);
            request.setAttribute("statesMap", statesMap);
            request.setAttribute("userStatusMap", userStatusMap);
            request.setAttribute("isRealMaps", isRealMap);
            request.setAttribute("onlineStatusMap", onlineStatusMap);
            request.setAttribute("states", states);
            request.setAttribute("status", status);
            request.setAttribute("isReal", isReal);
            request.setAttribute("onlineStatus", onlineStatus);
            request.setAttribute("windowTicketMap", windowTicketMap);
            request.setAttribute("windowTicket", windowTicket);
            request.setAttribute("vipStatusMap", vipStatusMap);
            request.setAttribute("vipStatus", vipStatus);
            request.setAttribute("nick", nick);


            if (!users.getId().equals(5)){
                request.setAttribute("type", 1);
            }else {
                request.setAttribute("type", 2);
            }

            List<AppNoPay> noPayList = appNoPayService.list();
            if(ObjectUtil.isNotNull(noPayList)){
                request.setAttribute("noPayList", noPayList);
            }
            return this.sucess("/web/after/user/userlist");
        } catch (Exception e) {
            logger.error("查看用户列表异常", e);
            return this.error("系统繁忙");
        }
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @RequestMapping("/detail")
    public String detail(Integer id) {
        try {
            AppUser user = appUserService.get(id);
            request.setAttribute("user", user);
            //实名状态
            Map<Integer, String> userStatusMap = Constants.getUserStatusMap();
            request.setAttribute("userStatusMap", userStatusMap);

            return this.sucess("/web/after/user/edit");
        } catch (Exception e) {
            logger.error("查看用户详情异常", e);
            return this.error("系统繁忙");
        }
    }

    /**
     * 发送朋友圈获取用户信息拉起界面
     */
    @RequestMapping("/sendDongTaiGetById")
    public String getUserId(Integer id) {
        try {
            AppUser user = appUserService.get(id);
            request.setAttribute("user", user);

            return this.sucess("/web/after/user/sendDongTai");
        } catch (Exception e) {
            logger.error("查看用户详情异常", e);
            return this.error("系统繁忙");
        }
    }

    /**
     * 更新用户的接口
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public Result del(AppUser appUser) {
        try {


            if (ObjectUtil.isEmpty(appUser.getPic())){
                Integer sex = appUserService.get(appUser.getId()).getSex();

                //默认头像
                //男
                if (sex.equals(1)) {
                    appUser.setPic("http://osszhiyuhn1.ptszyxx.com/default/nang.png");
                }
                //女
                if (sex.equals(2)) {
                    appUser.setPic("http://osszhiyuhn1.ptszyxx.com/default/nv.png");
                }

            }

            appUserService.save(appUser);
            return this.sucessJSON("保存成功");
        } catch (Exception e) {
            logger.error("保存成功信息异常", e);
            return this.errorJSON("系统繁忙");
        }
    }

    /**
     * 头像上传
     */
    @ResponseBody
    @RequestMapping("/upOssImg")
    public Result upOssImg(MultipartFile[] file) {
        try {
            String pic = "";
            //上传：循环获取file数组中得文件
            StringBuffer stringBuffer = new StringBuffer();
            if (file != null && file.length > 0) {
                MultipartFile files = file[0];
                if (!files.isEmpty()) {
                    //判断图片大小
                    if (files.getSize() <= 52428800) {
                        //上传阿里云oss
                        String ossUrl = userService.aliyunOssupload(files);
                        if (isNull(ossUrl)) {
                            return errorJSON("文件上传失败");
                        }
                        //拼接
                        stringBuffer.append(ossUrl);
                    } else {
                        return this.errorJSON("文件大小超过限制！");
                    }
                }
            } else {
                return this.errorJSON("文件不能为空");
            }
            pic = stringBuffer.toString();
            Map<String, String> mm = new HashMap<>();
            mm.put("name", pic);
            mm.put("imgname", "");
            return this.sucessJSON(mm, "保存成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }


    /**
     * 修改用户金额
     *
     * @param id
     * @return
     */
    @RequestMapping("/updateMoneyDetail")
    public String updateMoneyDetail(Integer id) {
        try {
            AppUser user = appUserService.get(id);
            //表单唯一吗
            String formToken = getNewFileNameuuid();
            redisUtil.set(FORMTOKEN + formToken, formToken, 60 * 30);
            request.setAttribute("formToken", formToken);
            request.setAttribute("user", user);
            return this.sucess("/web/after/user/moneyEdit");
        } catch (Exception e) {
            logger.error("查看用户详情异常", e);
            return this.error("系统繁忙");
        }
    }

    /**
     * 查看用户发布的动态
     *
     * @param id
     * @return
     */
    @RequestMapping("/dongtaiDetails")
    public String dongtaiDetails(Integer id, SplitPageBean splitPage) {
        try {
            AppUser user = appUserService.get(id);
            Map<String, Object> map = new HashMap<>(16);
            map.put("userid", user.getId());
            map.put("status", 1);
            List<AppDynamic> dtList = afterDynamicService.findList(map, splitPage);
            request.setAttribute("user", user);
            request.setAttribute("list", dtList);
            request.setAttribute("userid", user.getId());
            return this.sucess("/web/after/user/dtList");
        } catch (Exception e) {
            logger.error("查看用户详情异常", e);
            return this.error("系统繁忙");
        }
    }

    /**
     * 操作用户的金币余额
     *
     * @param userid 用户id
     * @param leixng 操作类型   1增加余额  2减少余额
     * @param money  操作金额
     * @return
     */
    @RequestMapping("/userJinUpdate")
    @ResponseBody
    public Result userJinUpdate(Integer userid, Integer leixng, Integer money, String jinbiRemark, String formToken) {
        try {
            if (StringEx.isNull(formToken)) {
                return this.errorJSON("表单异常,请刷新页面");
            }
            if (!redisUtil.hasKey(FORMTOKEN + formToken)) {
                return this.errorJSON("请勿重复提交");
            }
            if (StringEx.isNull(userid)) {
                return this.errorJSON("未获取到相关用户信息");
            }
            AppUser user = appUserService.get(userid);
            if (StringUtils.isEmpty(user)) {
                return this.errorJSON("用户不存在");
            }
            //修改余额
            if (leixng == 1) {
                jinbiRemark = "后台增加";
                appUserService.updatejinbi(userid, new BigDecimal(money.toString()));
            } else if (leixng == 2) {
                if (user.getJinbi().compareTo(new BigDecimal(String.valueOf(money))) < 0) {
                    return this.errorJSON("用户金币余额不足,无法扣除");
                }
                jinbiRemark = "后台减少";
                appUserService.upJianjinbi(userid, new BigDecimal(money.toString()));
            } else {
                return this.errorJSON("金币余额操作状态不支持");
            }
            //增加明细
            afterMingxiJinbiService.addMingxi(userid, new BigDecimal(money.toString()), leixng, jinbiRemark);
            redisUtil.del(FORMTOKEN + formToken);

            String key = String.format(RedisKeys.USER, userid);
            redisUtil.del(key);
            return this.sucessJSON("成功");
        } catch (Exception e) {
            logger.error("操作异常", e);
            return this.errorJSON("系统繁忙");
        }
    }

    /**
     * 操作用户的钻石余额
     *
     * @param userid 用户id
     * @param leixng 操作类型   1增加余额  2减少余额
     * @param money  操作金额
     * @return
     */
    @RequestMapping("/userZuanUpdate")
    @ResponseBody
    public Result userZuanUpdate(Integer userid, Integer leixng, Integer money, String formToken) {
        try {
            if (StringEx.isNull(formToken)) {
                return this.errorJSON("表单异常,请刷新页面");
            }
            if (!redisUtil.hasKey(FORMTOKEN + formToken)) {
                return this.errorJSON("请勿重复提交");
            }
            if (StringEx.isNull(userid)) {
                return this.errorJSON("未获取到相关用户信息");
            }
            AppUser user = appUserService.get(userid);
            if (StringUtils.isEmpty(user)) {
                return this.errorJSON("用户不存在");
            }
            String zuanshiRemark = "";
            money = Math.abs(money);
            //修改余额
            if (leixng == 1) {
                appUserService.updateZuanShi(userid, new BigDecimal(money.toString()));
                zuanshiRemark = "后台增加";
            } else if (leixng == 2) {
                if (user.getDiamonds().compareTo(new BigDecimal(String.valueOf(money))) < 0) {
                    return this.errorJSON("用户钻石数量不足,无法扣除");
                }
                appUserService.upJianZuanShi(userid, money);
                zuanshiRemark = "后台减少";
            } else {
                return this.errorJSON("钻石数量操作状态不支持");
            }
            //增加明细
            afterMingXiZuanShiService.addMingxi(userid, new BigDecimal(money.toString()), leixng, zuanshiRemark);
            redisUtil.del(FORMTOKEN + formToken);

            String key = String.format(RedisKeys.USER, userid);
            redisUtil.del(key);
            return this.sucessJSON("成功");
        } catch (Exception e) {
            logger.error("操作异常", e);
            return this.errorJSON("系统繁忙");
        }
    }


    /**
     * 账号状态修改
     *
     * @param id
     * @param state
     * @return
     */
    @RequestMapping("/upstate")
    @ResponseBody
    public Result jinyong(Integer id, Integer state, Integer hour) {
        try {
            if (StringEx.isNull(id)) {
                return this.errorJSON("未获取到相关用户信息");
            }
            if (StringEx.isNull(state)) {
                return this.errorJSON("状态不能为空");
            }
            Object oldToken = redisUtil.get(RedisKeys.userId + id);
            if (oldToken != null && oldToken.toString().trim().length() > 0) {
                redisUtil.del(RedisKeys.userToken + oldToken.toString().trim());
            }
            appUserService.upstate(id, state, hour);
            String key = String.format(RedisKeys.USER, id);
            redisUtil.del(key);
            String keys = String.format(RedisKeys.userIds, id);
            redisUtil.del(keys);
            return this.sucessJSON("保存成功");
        } catch (Exception e) {
            logger.error("账号解禁操作异常", e);
            return this.errorJSON("系统繁忙");
        }
    }


    /**
     * 账号状态修改
     *
     * @param id
     * @return
     */
    @RequestMapping("/upWhiteStatus")
    @ResponseBody
    public Result upWhiteStatus(Integer id, Integer state) {
        try {
            if (StringEx.isNull(id)) {
                return this.errorJSON("未获取到相关用户信息");
            }

            appUserService.upWhiteStatus(id, state);
            return this.sucessJSON("保存成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }


    /**
     * 粉丝列表
     *
     * @param splitPageBean
     * @param userid
     * @return
     */
    @RequestMapping("/fensi")
    public String fensi(SplitPageBean splitPageBean, String startDate, String endDate, Integer userid, String followednick, String followedphone) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("userid", userid);
            map.put("followednick", followednick);
            map.put("followedphone", followedphone);
            map.put("startDate", startDate);
            map.put("endDate", endDate);
            List<AppFollow> list = appFollowService.list(map, splitPageBean);

            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPageBean);
            request.setAttribute("followednick", followednick);
            request.setAttribute("followedphone", followedphone);
            request.setAttribute("userid", userid);
            request.setAttribute("startDate", startDate);
            request.setAttribute("endDate", endDate);
            return this.sucess("/web/after/user/fensiList");
        } catch (Exception e) {
            logger.error("查看粉丝列表异常", e);
            return this.error("系统繁忙");
        }
    }

    /**
     * 收到的礼物
     *
     * @param id
     * @param splitPage
     * @return
     */
    @RequestMapping("/liwu")
    public String liwu(Integer id, SplitPageBean splitPage) {
        try {
            if (StringUtils.isEmpty(id)) {
                return error("userid不能为空");
            }
            //收到的礼物
            List<AppMingXiShouLi> byUserid = afterMingXiShouLiService.findList(id, splitPage);
            request.setAttribute("liwu", byUserid);
            request.setAttribute("userid", id);
            request.setAttribute("splitPage", splitPage);
            return this.sucess("/web/after/user/liwu");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    /**
     * 当前坐骑装扮
     */
    @RequestMapping("/zuoqi")
    public String getDressup(Integer id, SplitPageBean splitPage) {
        try {
            if (StringUtils.isEmpty(id)) {
                return error("用户id不能为空");
            }
            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("userid", id);
            objectMap.put("type", 1);
            List<AppUserDressup> mones = afterUserDressupService.list(objectMap, splitPage);//坐骑
            Map<String, Object> mm = new HashMap<>();
            mm.put("mones", mones);
            request.setAttribute("mones", mones);
            return this.sucess("/web/after/user/zuoqi");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    /**
     * 当前头饰装扮
     */
    @RequestMapping("/headList")
    public String getHead(Integer id, SplitPageBean splitPage) {
        try {
            if (StringUtils.isEmpty(id)) {
                return error("用户id不能为空");
            }
            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("userid", id);
            objectMap.put("type", 2);
            List<AppUserDressup> head = afterUserDressupService.list(objectMap, splitPage);//头饰
            Map<String, Object> mm = new HashMap<>();
            mm.put("head", head);
            request.setAttribute("head", head);
            return this.sucess("/web/after/user/head");
        } catch (Exception e) {
            return this.error("系统繁忙");

        }
    }

    /**
     * 下级列表
     */
    @RequestMapping("/xiajiList")
    public String xiajiList(Integer id, SplitPageBean splitPage) {
        try {
            if (StringUtils.isEmpty(id)) {
                return error("用户id不能为空");
            }
            //邀请人
            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("userid", id);
            List<AppInvitationRecord> list = afterInvitationRecordService.findList(objectMap, splitPage);
            String userArr = "";
            for (AppInvitationRecord record : list) {
                userArr += "," + record.getUserid();
            }
            if (!isNull(userArr)) {
                userArr = userArr.substring(1);
            }
            //批量查询发布人信息
            Map<Integer, AppUser> userMap = new HashMap<>(16);
            if (!isNull(userArr)) {
                List<AppUser> userList = userService.getPiliang(userArr);
                for (AppUser user1 : userList) {
                    userMap.put(user1.getId(), user1);
                }
            }
            for (AppInvitationRecord invitationRecord : list) {
                AppUser user2 = userMap.get(invitationRecord.getUserid());
                invitationRecord.setLeijichongzhi(user2.getChongzhimoney());
                invitationRecord.setShimingstate(user2.getStatus());
            }
            //计算下级总累积充值金额
            AppUser user = afterUserService.get(id);
            AppUser zUser = afterUserService.xiajiZongCzMoney(user.getCode());
            Map<Integer, String> userStatusMap = Constants.getUserStatusMap();
            request.setAttribute("userStatusMap", userStatusMap);
            request.setAttribute("list", list);
            request.setAttribute("zongMoney", zUser.getChongzhimoney());
            request.setAttribute("splitPage", splitPage);
            request.setAttribute("id", id);
            return this.sucess("/web/after/user/xiajiList");
        } catch (Exception e) {
            return this.error("系统繁忙");

        }
    }

    /**
     * 用户流水
     *
     * @param id
     * @return
     */
    @RequestMapping("/liushui")
    public String liushui(Integer id) {
        try {
            if (StringUtils.isEmpty(id)) {
                return error("用户id不能为空");
            }
            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("rewardid", id);
            //抽奖的流水
            Long AppGameGiftRecord = afterGameGiftRecordService.findFlow(null, id);
            Long map = afterGiftRewardService.findListByInfo(objectMap);
            Map<String, Object> paraMap = new HashMap<>();
            paraMap.put("money", AppGameGiftRecord);
            paraMap.put("liwu", map);
            List list = new ArrayList();
            list.add(paraMap);
            request.setAttribute("list", list);
            return this.sucess("/web/after/user/liushui");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    /**
     * 用户兑换记录
     *
     * @param splitPage
     * @param userid
     * @return
     */
    @RequestMapping("/dhlist")
    public String list(SplitPageBean splitPage, Integer userid) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("userid", userid);
            List<AppMingxiDuihuan> list = afterMingxiDuihuanService.findList(map, splitPage);
            request.setAttribute("list", list);
            request.setAttribute("splitPage", splitPage);
            request.setAttribute("userid", userid);
            return this.sucess("/web/after/user/dhlist");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }


    /**
     * 修改用户语音直播权限
     *
     * @Param: [id 用户id, state 开启状态（1，开启，2，关闭）]
     * @return: com.vf.admin.common.entity.Result
     * @Author: zhouyunyi
     * @Date: 2021/07/23
     * @Time: 15:34
     */
    @RequestMapping("/upyuyin")
    @ResponseBody
    public Result upstateyuyinStatus(Integer id, Integer state) {
        try {
            if (StringEx.isNull(id)) {
                return this.errorJSON("获取ID为空");
            }
            AppUser user = appUserService.get(id);
            if (user.getShipinstate() != 1) {
                return this.errorJSON("该用户还未通过视频认证");
            }
            if (user.getStatus() != 1) {
                return this.errorJSON("该用户还未通过实名认证");
            }

            appUserService.upstateYuyinStatus(id, state);

            return this.sucessJSON("保存成功");
        } catch (Exception e) {
            logger.error("修改用户视频直播权限时异常", e);
            return this.errorJSON("系统繁忙");
        }
    }

    /**
     * 关闭用户视频直播权限
     *
     * @Param: [id 用户id, state 开启状态(1，开启，2，关闭)]
     * @return: com.vf.admin.common.entity.Result
     * @Author: zhouyunyi
     * @Date: 2021/07/23
     * @Time: 17:31
     */
    @RequestMapping("/upvideo")
    @ResponseBody
    public Result upstatevideoStatus(Integer id, Integer state) {
        try {
            if (StringEx.isNull(id)) {
                return this.errorJSON("获取ID为空");
            }
            if (StringEx.isNotNull(state)) {//关闭
                appUserService.upstateVideoStatus(id, state);
            }
            return this.sucessJSON("保存成功");
        } catch (Exception e) {
            logger.error("关闭用户视频直播权限异常", e);
            return this.errorJSON("系统繁忙");
        }
    }

    @RequestMapping("/selectHour")
    public String selectHour(Integer userId) {
        try {

            Map<Integer, String> closeMap = Constants.getCloseMap();


            request.setAttribute("closeMap", closeMap);

            request.setAttribute("userId", userId);

            return this.sucess("/web/after/user/selectHour");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    @RequestMapping("/gotoGoodUserCode")
    public String gotoGoodUserCode(Integer userId) {
        try {
            request.setAttribute("userId", userId);
            return this.sucess("/web/after/user/goodUserCode");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    @RequestMapping("/goodUserCode")
    @ResponseBody
    public Result goodUserCode(Integer userId, Integer goodUserCode) {
        try {
            if (goodUserCode.toString().length() < 6) {
                return this.errorJSON("靓号长度最低六位！");
            }
            AppUser goodCodeExistUserId = afterUserService.getByUserCodeOrGoodCodeExistUserId(userId, goodUserCode);
            if (goodCodeExistUserId != null) {
                return this.errorJSON("当前靓号已被使用，请更换其他靓号！");
            }

            AppUser appUser = appUserService.get(userId);
            appUser.setGoodusercode(goodUserCode);
            appUserService.updateGoodUserCode(userId, goodUserCode);
            return this.sucessJSON("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("系统繁忙");
        }
    }
}
